package com.lufei.cardin.controller;

import lombok.extern.log4j.Log4j2;
import org.attoparser.util.TextUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController("/test")
@Log4j2
public class TestController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello Spring Security!";
    }

    @PostMapping("/getMouseData")
    public String getMouseData(@RequestParam int isRunning){
        log.info("是否运行: " + isRunning);
        return "Hello Spring Security!";
    }


    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(@RequestParam(required = false) String message) {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        if (null!=message) {
            messageData = message;
        }

        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }


}
